<?php

namespace App\Transformers;

use App\Models\User;
use League\Fractal\TransformerAbstract;

class UserTransformer extends TransformerAbstract
{
    protected $availableIncludes = ['prizeRecords', 'roles'];
    public function transform(User $user)
    {
        return [
            'id' => $user->id,
            'name' => $user->name,
            'nickname' => $user->nickname,
            'realname' => $user->realname,
            'phone' => $user->phone,
            'address' => $user->address,
            'avatar' => $user->avatar,
            'introduction' => $user->introduction,
            'created_at' => $user->created_at->toDateTimeString(),
            'updated_at' => $user->updated_at->toDateTimeString(),
        ];
    }

    public function includePrizeRecords(User $user)
    {
        $prizeRecords = $user->prizeRecords;

        return $this->collection($prizeRecords, new PrizeRecordTransformer());
    }

    public function includeRoles(User $user)
    {
        return $this->collection($user->roles, new RoleTransformer());
    }
}